# ----------------------------------------------------------------------------------
# Microsoft Developer & Platform Evangelism
# 
# Copyright (c) Microsoft Corporation. All rights reserved.
# 
# THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
# OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
# ----------------------------------------------------------------------------------
# The example companies, organizations, products, domain names,
# e-mail addresses, logos, people, places, and events depicted
# herein are fictitious.  No association with any real company,
# organization, product, domain name, email address, logo, person,
# places, or events is intended or should be inferred.
# ----------------------------------------------------------------------------------
#
# THIS IS SAMPLE CODE. BUGS ARE EXPECTED. SECURITY IS NOT.
#
# This module reads all status change notifications
#

#name of result queue (Running, Completed, Failed, actually a service bus topic)
rqname = 'outqueue'
#service bus namespace
sbname='gerespy'
#service bus key
sbkey=' your service bus key'
#service bus issuer, typically owner
sbissuer='owner'


from time import sleep
from azure.servicebus import *


#open connection to azure storage queue and table and service bus
bus_service = ServiceBusService(service_namespace=sbname, account_key=sbkey, issuer=sbissuer)


while True:
    try:
        msg=bus_service.receive_subscription_message(rqname,'AllResults')
        m=str(msg.custom_properties['jobid'])        
        print('JobID: '+m+' '+msg.body)
        #According to documentation, reading the message should delete it by default. There is a bug in SDK documentation, so delete explicitly
        msg.delete()
        
    except KeyError as error:
        print('No more notifications. Waiting...')    
        sleep(10)
